package com.fjh.shop.marketing.service.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjh.shop.common.PageResult;
import com.fjh.shop.common.Result;
import com.fjh.shop.marketing.model.entity.Activity;
import com.fjh.shop.marketing.service.service.ActivityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuyunbin
 * @since 2025-07-16
 */
@RestController
@RequestMapping("/activity")
@Tag(name = "营销活动管理", description = "营销活动的CRUD操作")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    @PostMapping
    @Operation(summary = "新增活动", description = "创建一个新的营销活动")
    public Result<Activity> addActivity(@RequestBody Activity activity) {
        Activity savedActivity = activityService.addActivity(activity);
        return Result.success(savedActivity);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除活动", description = "根据ID删除营销活动")
    public Result<Boolean> deleteActivity(@PathVariable @Parameter(description = "活动ID") Long id) {
        boolean result = activityService.deleteActivity(id);
        return Result.success(result);
    }

    @PutMapping
    @Operation(summary = "更新活动", description = "更新营销活动信息")
    public Result<Activity> updateActivity(@RequestBody Activity activity) {
        Activity updatedActivity = activityService.updateActivity(activity);
        return Result.success(updatedActivity);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取活动详情", description = "根据ID获取营销活动详情")
    public Result<Activity> getActivityById(@PathVariable @Parameter(description = "活动ID") Long id) {
        Activity activity = activityService.getActivityById(id);
        return Result.success(activity);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询活动", description = "分页查询营销活动，支持多条件查询")
    public PageResult<Activity> pageActivity(
            @RequestParam(defaultValue = "1") @Parameter(description = "当前页码") Integer current,
            @RequestParam(defaultValue = "10") @Parameter(description = "每页记录数") Integer pageSize,
            @RequestParam(required = false) @Parameter(description = "活动名称") String name,
            @RequestParam(required = false) @Parameter(description = "活动类型") Integer type,
            @RequestParam(required = false) @Parameter(description = "开始时间") String startTime,
            @RequestParam(required = false) @Parameter(description = "结束时间") String endTime,
            @RequestParam(required = false) @Parameter(description = "活动状态") Integer status
    ) {
        IPage<Activity> page = activityService.pageActivity(
                current, pageSize, name, type, startTime, endTime, status);
        return PageResult.success(page);
    }
}

